package com.hou.shop.product.service.impl;

import com.hou.shop.product.dao.CategoryBrandRelationDao;
import com.hou.shop.product.entity.CategoryBrandRelationEntity;
import com.hou.shop.product.service.CategoryBrandRelationService;
import feign.template.QueryTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hou.common.utils.PageUtils;
import com.hou.common.utils.Query;

import com.hou.shop.product.dao.BrandDao;
import com.hou.shop.product.entity.BrandEntity;
import com.hou.shop.product.service.BrandService;
import org.springframework.util.StringUtils;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;

    // 带条件查询获取分页信息
    @Override
    public PageUtils queryPage(Map<String, Object> params, String key) {

        QueryWrapper<BrandEntity> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and((obj) -> {
                obj.like("name", key).
                        or().like("descript", key).
                        or().like("first_letter", key).
                        or().eq("brand_id", key);
            });
        }

        // 如果有参数则进行模糊查询，没有获取全部数据
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    // 保证其他表冗余数据能够同步
    @Override
    public void updateDetail(BrandEntity brand) {
        Long brandId = brand.getBrandId();
        String brandName = brand.getName();

        // 更新自己的表
        this.updateById(brand);

        if (!StringUtils.isEmpty(brandName)) {
            // 更新品牌与分组表
            categoryBrandRelationService.updateBrandName(brandId,brandName);

            //TODO 其他关联更新
        }

    }

}